package com.yxx.data.controller;



import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yxx.data.domain.DiseaseInformation;
import com.yxx.data.service.DiseaseInformationService;
import com.yxx.data.vo.DiseaseInformationVo;
import com.yxx.sys.common.AppSecureUtils;
import com.yxx.sys.common.DataGridView;
import com.yxx.sys.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yxx
 * @since 2022-06-29
 */
@RestController
@RequestMapping("/disease-information")
@Api("疾病信息控制器")
public class DiseaseInformationController {
  @Autowired
  DiseaseInformationService diseaseInformationService;

  @GetMapping("/loadAllInformation")
  @ApiOperation("查询疾病信息")
  public DataGridView loadAllInformation(DiseaseInformationVo diseaseInformationVo) {
    IPage<DiseaseInformation> page = new Page<DiseaseInformation>(diseaseInformationVo.getPage(), diseaseInformationVo.getLimit());
    QueryWrapper<DiseaseInformation> queryWrapper = new QueryWrapper<DiseaseInformation>();

    queryWrapper.eq(StrUtil.isNotBlank(diseaseInformationVo.getHospitalId()), "hospital_id", diseaseInformationVo.getHospitalId());
    queryWrapper.like(StrUtil.isNotBlank(diseaseInformationVo.getHurtReason()), "hurt_reason", diseaseInformationVo.getHurtReason());
    queryWrapper.like(StrUtil.isNotBlank(diseaseInformationVo.getHurtPosition()), "hurt_position", diseaseInformationVo.getHurtPosition());
    queryWrapper.eq(diseaseInformationVo.getDepth() != null && diseaseInformationVo.getDepth() != -1, "depth", diseaseInformationVo.getDepth());
    queryWrapper.orderByDesc("id");
    diseaseInformationService.page(page, queryWrapper);
    return new DataGridView(page.getTotal(), page.getRecords());
  }

  @PostMapping("/addInformation")
  @ApiOperation("添加疾病信息")
  public Result addInformation(DiseaseInformationVo diseaseInformationVo) {
    diseaseInformationVo.setName(AppSecureUtils.getEncryptData(diseaseInformationVo.getName()));

    diseaseInformationVo.setCreatedTime(LocalDate.now());
    diseaseInformationVo.setChangeTime(LocalDate.now());
    boolean succ = diseaseInformationService.save(diseaseInformationVo);
    if(!succ) return Result.ADD_REEOR;
    return Result.ADD_SUCCESS;
  }

  @PostMapping("/updateInformation")
  @ApiOperation("更新疾病信息")
  public Result updateInformation(DiseaseInformationVo diseaseInformationVo) {
    try {
      boolean b = diseaseInformationService.updateById(diseaseInformationVo);
      if(!b) return Result.UPDATE_REEOR;
      return Result.UPDATE_SUCCESS;
    } catch (Exception e) {
      e.printStackTrace();
      return Result.UPDATE_REEOR;
    }
  }

  @PostMapping("/deleteInformation")
  @ApiOperation("删除疾病信息")
  public Result deleteInformation(Integer id) {
    try {
      diseaseInformationService.removeById(id);
      return Result.DELETE_SUCCESS;
    } catch (Exception e) {
      e.printStackTrace();
      return Result.DELETE_REEOR;
    }
  }
}
